﻿app.controller('StoreController', function ($scope, $http, $location, $routeParams, requestService) {
    console.log('StoreController');
    var page = 1;

    $scope.productKey = $scope.productKey ? $scope.productKey : '';
    $scope.products = [];
    $scope.query = function (key, page) {
        requestService.lists(key, page).then(function (data) {
            if (data._code === 200) {
                for (var i = 0; i < data._data.length; i++) {
                    $scope.products.push(data._data[i]);
                };
            };
        });
        console.log($scope.products);
    };
    $scope.query('', page)
    //查询
    $scope.search = function () {
        $scope.products = [];
        $scope.query($scope.productKey, page)
    };
    $scope.add = function () {
        $location.url('/add');
    };
    $scope.loadMore = function () {
        page++;
        $scope.query($scope.productKey, page)
    };
    $scope.delte = function (id) {
        requestService.product_delete(id).then(function (data) {
            console.log(data);
            if (data._code == 200) {
                console.log('删除成功');
                console.log(data._data);
            }
        });
    };
    $scope.edit = function (id) {
        $location.url('/edit/' + id);
    };
});

app.controller('OrderController', function ($scope, $http, $location, $routeParams, requestService) {
    console.log('OrderController');
    $scope.product = {
        Id: '',
        Name: '',
        Price: 0
    };
    var id = $routeParams.id;
    $scope.btn = id ? '确定' : '添加';

    $scope.add = function () {
        if (id) {
            $scope.product.Id = id;
            requestService.product_put($scope.product).then(function (data) {
                console.log(data);
                if (data._code == 200) {
                    console.log('修改成功');
                    console.log(data._data);
                }
            });
        } else {
            requestService.product_add($scope.product).then(function (data) {
                console.log(data);
                if (data._code == 200) {
                    console.log('添加成功');
                    console.log(data._data);
                    $location.url("/")
                }
            });
        };

    };
});


